package com.chookss.home.update;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;

import androidx.core.content.FileProvider;

import com.chookss.BuildConfig;
import com.chookss.MyApp;

import java.io.File;

/**
 * <pre>
 *     author : jiaam
 *     time   : 2020/08/27
 *     desc   :
 * </pre>
 */
public class InstallLocalApk {
    public static void install(Context mContext, File file) {
        if (mContext == null) {
            return;
        }
        MyApp.isDownApk = false;
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
        try {
            Uri updateUri = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                updateUri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".fileProvider", file);
            } else {
                updateUri = Uri.fromFile(file);
            }
            intent.setDataAndType(updateUri,
                    "application/vnd.android.package-archive");
            mContext.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
